package com.hospital.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hospital.entity.Schedule;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

/**
 * 排班Mapper接口
 *
 * @author Hospital Management System
 */
@Mapper
public interface ScheduleMapper extends BaseMapper<Schedule> {

    /**
     * 查询医生排班列表（带关联信息）
     */
    @Select("SELECT s.*, u.name as doctor_name, d.name as department_name " +
            "FROM tb_schedule s " +
            "LEFT JOIN tb_user u ON s.doctor_id = u.id " +
            "LEFT JOIN tb_department d ON s.department_id = d.id " +
            "WHERE s.doctor_id = #{doctorId} " +
            "AND s.schedule_date >= #{startDate} " +
            "AND s.schedule_date <= #{endDate} " +
            "ORDER BY s.schedule_date, s.time_slot")
    List<Schedule> getDoctorSchedules(@Param("doctorId") Long doctorId, 
                                    @Param("startDate") LocalDate startDate, 
                                    @Param("endDate") LocalDate endDate);

    /**
     * 查询全院排班列表（管理员用）
     */
    @Select("SELECT s.*, u.name as doctor_name, d.name as department_name " +
            "FROM tb_schedule s " +
            "LEFT JOIN tb_user u ON s.doctor_id = u.id " +
            "LEFT JOIN tb_department d ON s.department_id = d.id " +
            "WHERE s.schedule_date >= #{startDate} " +
            "AND s.schedule_date <= #{endDate} " +
            "ORDER BY s.schedule_date, s.department_id, s.time_slot")
    List<Schedule> getAllSchedules(@Param("startDate") LocalDate startDate, 
                                 @Param("endDate") LocalDate endDate);

    /**
     * 按科室查询排班
     */
    @Select("SELECT s.*, u.name as doctor_name, d.name as department_name " +
            "FROM tb_schedule s " +
            "LEFT JOIN tb_user u ON s.doctor_id = u.id " +
            "LEFT JOIN tb_department d ON s.department_id = d.id " +
            "WHERE s.department_id = #{departmentId} " +
            "AND s.schedule_date >= #{startDate} " +
            "AND s.schedule_date <= #{endDate} " +
            "ORDER BY s.schedule_date, s.time_slot")
    List<Schedule> getSchedulesByDepartment(@Param("departmentId") Long departmentId, 
                                          @Param("startDate") LocalDate startDate, 
                                          @Param("endDate") LocalDate endDate);

    /**
     * 查询排班统计信息
     */
    @Select("SELECT " +
            "COUNT(*) as total_schedules, " +
            "SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as normal_schedules, " +
            "SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) as cancelled_schedules, " +
            "SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as full_schedules " +
            "FROM tb_schedule " +
            "WHERE schedule_date >= #{startDate} AND schedule_date <= #{endDate}")
    List<Object> getScheduleStatistics(@Param("startDate") LocalDate startDate, 
                                     @Param("endDate") LocalDate endDate);
} 